
let time = require('../../utils/util.js');
let app = getApp();
Page({
  data: {
    winHeight: 500,
    currentTab: 0,
    iconFlag:false,
    salesmanName: '',
    supervisorName:'',
    bumenmingcheng:'',
    postFlag:false,
    startTime: '',
    endTime: '',
    openid:'',
    navData: [{
      name: "首页",
      current: 0,
      style: 0,
      ico: 'icon-deng',
      fn: 'navJumpPage',
      id: '/pages/home/home'
    }, {
      name: "客户",
      current: 0,
      style: 0,
      ico: 'icon-guanlikehu',
      fn: 'navJumpPage',
      id: '/pages/client/client'
    }, {
      name: "添加汇报",
      current: 0,
      style: 1,
      ico: '',
      fn: 'navJumpPage',
      id: '/pages/addReport/addReport'
    }, {
      name: "工作汇报",
      current: 1,
      style: 0,
      ico: 'icon-xiebaogao',
      fn: 'navJumpPage',
      id: ''
    }, {
      name: "我的",
      current: 0,
      style: 0,
      ico: 'icon-dkw_wode',
      fn: 'navJumpPage',
      id: '/pages/salesmanMine/salesmanMine'
    }],
    dataList: [],
    subDataList:[]
  },
  bindChange: function(e) {
    this.setData({
      currentTab: e.detail.current
    });
    e.detail.current == '0' ? this.loadList() : this.subLoadList();
  },
  swichNav(e) {
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //顶部nav 跳转
  navJumpPage(event) {
    let url = event.currentTarget.id;
    if (url) {
      if (url === '/pages/addReport/addReport') {
        wx.navigateTo({
          url: url
        });
      } else {
        wx.redirectTo({
          url: url
        });
      }
    }
  },
  onShow(){
    this.onLoad();
  },
  onLoad() {
    let that = this,
      newDate = new Date(),
      startTime = time.formatTime(newDate); //获取时间戳
    wx.getStorage({
      key: 'openid',
      success(res) {
        that.setData({
          openid: res.data,
          startTime: that.Month(),
          endTime: startTime
        });
        that.loadList();
      },
    });
    //判断是否为部门主管
    wx.getStorage({
      key: 'post',
      success(res) {        
        let postFlag = false;
        res.data === '部门主管' ? postFlag = true : postFlag = false;
        that.setData({
          postFlag: postFlag
        });
      }
    });

    //获取姓名信息
    wx.getStorage({
      key: 'supervisorName',
      success(res) {
        that.setData({
          supervisorName: res.data
        });
      },fail(res){
        that.setData({
          supervisorName: ''
        });
      }
    });

    //获取职位信息
    wx.getStorage({
      key: 'bumenmingcheng',
      success(res) {
        that.setData({
          bumenmingcheng: res.data
        });
      }
    });

    //获取高度
    wx.getSystemInfo({
      success(res) {
        that.setData({
          winHeight: res.windowHeight
        })
      }
    });  
  },
  //加载个人汇报
  loadList() {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    app.get('GetGenZong',{
      action:'getGeRenGenZong',
      openid: that.data.openid,
      dtstart: that.data.startTime, //开始时间
      dtend: that.data.endTime, //结束时间
    }).then((res)=>{
      wx.hideLoading();
      that.setData({
        dataList: res
      });     
    })
  },
  //下级汇报
  subLoadList() {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    app.get('GetGenZong', {
      action: 'getXiaJiGenZong',
      ZhuGuanXingMing: that.data.supervisorName,
      YeWuYuanXingMing: that.data.salesmanName,
      BuMenMingCheng: that.data.bumenmingcheng,
      openid: that.data.openid,
      dtstart: that.data.startTime, //开始时间
      dtend: that.data.endTime, //结束时间
    }).then((res) => {
      wx.hideLoading();
      that.setData({
        subDataList: res
      });
    })
  },
  //开始时间
  startDate(e) {
    this.setData({
      startTime: e.detail.value
    })
  },
  //结束时间
  endDate(e) {
    this.setData({
      endTime: e.detail.value
    });
  },
  salesmanName(e) {
    this.setData({
      salesmanName: e.detail.value
    });
  },
  inquireBtn() {
    let flag = '';
    flag = !this.data.iconFlag;
    this.setData({
      iconFlag: flag
    })
  },
  formReset() {
    this.setData({
      startTime: '',
      endTime: ''
    });
    app.toast('搜索条件已重置');
  },
  formSubmit(e) {
    let formData = e.detail.value,
      startTime = formData.startTime,
      endTime = formData.endTime;
    if (!startTime) {
      app.toast('请选择起始时间');
    } else if (!endTime) {
      app.toast('请选择结束时间');
    } else {
      this.setData({
        startTime: startTime,
        endTime: endTime,
      });
      if (this.data.currentTab == '0') {
        this.loadList();
      } else {
        this.subLoadList();
        this.setData({
          salesmanName: formData.name
        });
      }
    }
  },
  itemBind(e){
    console.log(e)
    let that = this,
      flag = e.currentTarget.dataset.flag;
    if (flag ==='sub'){
      app.get('GetGenZong', {
        action: 'yueLan',
        openid: that.data.openid,
        genzongid: e.currentTarget.id,
      }).then((res) => {
        that.loadList();       
      });
      };
    wx.navigateTo({
      url: '/pages/reportDetail/reportDetail?id=' + e.currentTarget.id + '&flag=' + flag
    });
  },
  //获取当前月的后2两个月
  Month(month) {
    let date = new Date();
    var year = date.getFullYear(); //年
    var month = date.getMonth() - 1; //月 1个月 month从0开始，所以要加1
    if (month > 12) {
      year++;
      month -= 12;
    }
    if (month < 10) {
      month = "0" + month;
    }
    var date2 = new Date(year, month, 0); //新的年月
    var day1 = date.getDate();
    var day2 = date2.getDate();
    if (day1 > day2) { //防止+6月后没有31天
      day1 = day2;
    }
    let str = year + '-' +
      month + '-' +
      day1;
    return str;
  },
  
})